package opc_service

import (
	"beta/models"
	"beta/pkg/e"
	"beta/pkg/logger"
	"beta/pkg/util"
	"errors"
	"github.com/tidwall/gjson"
	"strings"
	"time"
)

type OpcService struct {
	FullName string
	CustomerID string
	Operator string
	ReceiverMobile string
	ReceiverEmail string
	DepositNum string
	Token string
	Hosts map[string]*models.AppEnv
	TraceId string
	IdentityID int
}

// 账户
type Account struct {
	CustomerID      string   `json:"customerId"`
	ServiceType     string   `json:"serviceType"`
	ProductIDList   []string `json:"productIdList"`
	InvoiceProperty string   `json:"invoiceProperty"`
	SettleType     string   `json:"settleType"`
	PlatformType    string   `json:"platformType"`
}

var (
	appName = "opc"
)

func (opc OpcService) AddAccount(accountProductId string) (accountId string, err error) {

	host := opc.Hosts[appName].AppDomain
	uri := "/opc/v1/account/openAccount"
	url := host + uri
	account := Account{
		CustomerID:  opc.CustomerID,
		ServiceType: "01",
		//ProductIDList:   []string{productId},
		ProductIDList:   strings.Split(accountProductId, ","),
		InvoiceProperty: "1",
		SettleType:     "2",
		PlatformType:    "1",
	}
	logger.ZapLogger.Errorw("【创建账户】", "请求参数", account, "traceId", opc.TraceId)

	cookie := util.GetCookie(util.BossCookieName, opc.Token)
	httpClient := util.NewHttpClientWithCookie(3*time.Second, cookie)
	resp, respErr := httpClient.R().SetHeaders(e.GetHeaderMap(e.JsonHeader)).
		SetBody(account).Post(url)
	logger.ZapLogger.Errorw("【创建账户】", "请求结果", resp, "traceId", opc.TraceId)

	if respErr != nil {
		return "", respErr
	}

	accountId = gjson.Get(resp.String(), "data").String()

	logger.ZapLogger.Infow("【创建客户】", "accountId", accountId,
		"traceId", opc.TraceId)
	return accountId, nil
}

type Remittance struct {
	AccountName string `json:"accountName"`
	AccountNo   string `json:"accountNo"`
	AmountEnd   string `json:"amountEnd"`
	AmountStart string `json:"amountStart"`
	DateEnd     string `json:"dateEnd"`
	DateStart   string `json:"dateStart"`
	PageNo      int64  `json:"pageNo"`
	PageSize    int64  `json:"pageSize"`
}

func (opc OpcService) QueryRemittance(fullName, bankId string) (remittanceId string, err error) {
	host := opc.Hosts[appName].AppDomain
	url := host + "/opc/v1/pay/queryRemittance"

	remittanceQuery := Remittance{
		AccountName: fullName,
		AccountNo:   bankId,
		AmountEnd:   "",
		AmountStart: "",
		DateEnd:     "",
		DateStart:   "",
		PageNo:      1,
		PageSize:    20,
	}

	logger.ZapLogger.Infow("【查询流水】", "请求参数", remittanceQuery, "traceId", opc.TraceId)
	cookie := util.GetCookie(util.BossCookieName, opc.Token)
	httpClient := util.NewHttpClientWithCookie(3 * time.Second, cookie).R()
	resp, respErr := httpClient.SetHeaders(e.GetHeaderMap(e.JsonHeader)).
		SetBody(remittanceQuery).Post(url)

	logger.ZapLogger.Infow("【查询流水】", "请求结果", resp, "traceId", opc.TraceId)

	if respErr != nil {
		return "", respErr
	}

	remittanceIdList := gjson.GetBytes(resp.Body(), "data.list")
	length := len(remittanceIdList.Array())
	if length == 0 {
		return "", errors.New("流水列表不能为空")
	}

	remittanceId = gjson.Get(remittanceIdList.Array()[0].String(), "remittanceId").String()
	return remittanceId, nil

}
